//获取元素
var wrap = document.getElementById("game");
var start = document.getElementById("menu");
var head = document.getElementById("head");
var bird = document.getElementById("bird");
var end = document.getElementById("end");
var pipeUl = document.getElementById("pipes");
var score = document.getElementById("score");
var bullet = document.getElementById("bullet");
var gameSound = document.getElementById("gamemusic");
var overSound = document.getElementById("gameover");
//分数
var num = 0;
//鸟上升定时器
var birdUpTimer;
//鸟下降定时器
var birdDownTimer;
//小鸟的速度
var speed = 0;
start.onclick = function(e) {
	var even = e || event;
	even.stopPropagation(); //阻止冒泡
	even.cancelBubble = true; //IE阻止冒泡
	head.style.display = "none";
	bird.style.display = "block";
	start.style.display = "none";
	score.style.display = "block";
	//播音乐
	gameSound.loop = "loop";
	gameSound.play();
	//生成罐道
	setInterval(creatPipe, 3000);
	//小鸟下落
	birdDownTimer = setInterval(birdDown, 20);
	//游戏区域的点击事件
	wrap.onclick = gameClick;
	//碰撞检测
	setInterval(function() {
		//获取到对应的li
		var lis = document.querySelectorAll("li");
		for(var i = 0; i < lis.length; i++) {
			//判断顶部桶是否与鸟发生碰撞
			collision(lis[i].firstElementChild);
			//判断底部桶是否与鸟发生碰撞
			collision(lis[i].lastElementChild)
		}
	}, 10)
}

function creatPipe() {
	//创建li
	var li = document.createElement("li");
	li.className = "pipe";
	li.style.left = pipeUl.offsetWidth + "px";
	pipeUl.appendChild(li);
	var passHeight = 120;
	var minHeight = 60; //管道的最小高度;
	//随机区间[minHeight,li.clientHeight-passHeight-minHeight]
	var topHeight = Math.floor(Math.random() * (li.clientHeight - passHeight - 2 * minHeight + 1) + minHeight);
	var bottomHeight = li.clientHeight - passHeight - topHeight;
	//分别创建两个div
	var topdiv = document.createElement("div");
	topdiv.className = "uppipe";
	topdiv.style.height = topHeight + "px";
	li.appendChild(topdiv);
	var bottomdiv = document.createElement("div");
	bottomdiv.className = "downpipe";
	bottomdiv.style.height = bottomHeight + "px";
	li.appendChild(bottomdiv);
	//移动管道
	//声明一个变量,存储不断修改的left值
	var x = pipeUl.clientWidth;
	var pipeMoveTimer = setInterval(function() {
		x--;
		li.style.left = x + "px";
		if(x < -li.clientWidth) {
			clearInterval(pipeMoveTimer);
			pipeUl.removeChild(li);

		}
		if(x == 0) {
			changScore();
		}
	}, 10);
}
//小鸟上升
function birdUp() {
	bird.src = "img/up_bird.png";
	speed--;
	if(speed < 0) {
		clearInterval(birdUpTimer);
		speed = 0;
		birdDownTimer = setInterval(birdDown, 20)

	}
	bird.style.top = bird.offsetTop - speed + "px";
	if(bird.offsetTop <= 0) {
		gameOver();
	}
}
//小鸟下落
function birdDown() {
	bird.src = "img/down_bird.png";
	speed += 0.5;
	if(speed > 5) {
		speed = 5;
	}
	bird.style.top = bird.offsetTop + speed + "px";
	//判断小鸟是否坠地
	if(bird.offsetTop >= 423 - bird.clientHeight) {
		bird.style.top = 423 - bird.clientHeight + "px";
		gameOver()
	}
	
}
//屏幕点击事件
function gameClick() {
	bullet.play();
	//清除当前时刻的定时器
	clearInterval(birdUpTimer);
	clearInterval(birdDownTimer);
	speed = 10;
	birdUpTimer = setInterval(birdUp, 20)
}

function collision(pipe) {
	//获取小鸟的位置信息
	var top1 = bird.offsetTop;
	var bottom1 = top1 + bird.offsetHeight;
	var left1 = bird.offsetLeft;
	var right1 = left1 + bird.offsetWidth;
	//获取桶的位置信息
	var top2 = pipe.offsetTop;
	var bottom2 = top2 + pipe.offsetHeight;
	var left2 = pipe.offsetParent.offsetLeft;
	var right2 = left2 + pipe.offsetWidth;
	if(!(bottom1 < top2 || left1 > right2 || top1 > bottom2 || right1 < left2)) {
		gameOver();
	}
}
var img1 = document.createElement("img");
var img2 = document.createElement("img");

function changScore() {
	num++;
	//	scoreImg.src="img/"+num/10+".jpg";
	scoreImg.src = "img/" + num % 10 + ".jpg"
	if(num > 9 && num <= 99) {
		score.insertBefore(img1, scoreImg);
		img1.src = "img/" + Math.floor(num / 10) + ".jpg";
	}
	if(num > 99) {
		score.insertBefore(img1, scoreImg);
		score.insertBefore(img2, img1);
		img1.src = "img/" + Math.floor(num / 10 % 10) + ".jpg";
		img2.src = "img/" + Math.floor(num / 100) + ".jpg";
	}
}
/*
var ends;
function gameOver() {
	end.style.display = "block";
	again.style.display = "block";
	end.style.zIndex = 10;
	currentScore.innerHTML = num;
	wrap.onclick = null;
//	gameSound.stop();
	ends = setInterval(function() {}, 10);
	var start = (ends - 50) > 0 ? ends - 50 : 0;
	for(var i = start; i <= ends; i++) {
		clearInterval(i);
	}
}
window.clearInterval(ends);
*/
again.onclick = function(){
	location.reload();
}

function gameOver() {
	gameSound.pause();
	overSound.play();
	//当王爷加载完成之后,所有的定时器的id是递增的,所以只要获取最后一个定时器的id,就可以得到所有定时器的id
	var index = setInterval(function(){},1000);
	for (var i=1;i<=index;i++) {
		clearInterval(i);
	}
	wrap.onclick = null;
	end.style.display = "block";
	end.style.zIndex = 10;
	currentScore.innerHTML = num;
	again.style.display = "block";
}
